package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 编辑课程界面
 *
 * @author yingchunli
 */
public class ModifyCourseActivity extends BaseSoocActivity {

    CallNameDao dao;
    @InjectView(R.id.toolbar_righttv)
    TextView toolbarRighttv;
    @InjectView(R.id.courseName_addcourse)
    ClearEditText courseName;//课程名称
    @InjectView(R.id.courseTime_addcourse)
    TextView courseTime;//上课时间
    String kcid;//课程
    String kcmc;//课程名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_modifycourse);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        kcid = getIntent().getStringExtra("kcid");//获取kcid
        kcmc = getIntent().getStringExtra("kcmc");//获取课程名称
        //设置课程名称  上课时间
        courseName.setText(getIntent().getStringExtra("kcmc"));
        courseTime.setText(getIntent().getStringExtra("skxq"));

        toolbarRighttv.setVisibility(View.VISIBLE);
        toolbarRighttv.setText("完成");
        //设置光标位置
        courseName.setSelection(courseName.getText().toString().length());


        //按键完成监听
        courseName.setImeOptions(EditorInfo.IME_ACTION_DONE);
        courseName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    onToolbarRightClick();
                }
                return false;
            }
        });
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            switch (requestCode) {
                case CodeConst.EXPAND_MODIFY_COURSE://编辑课程成功
                    if (dao.getStatus().equals("0")) {
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        MessageUtils.showImageShortToast(this, dao.getStatusCode());
                    }
                    break;
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "编辑课程";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeft() {
        finish();
    }

    @Override
    public boolean setToolbarRighttv(TextView toolbarRighttv) {
        return true;
    }

    /**
     * Toolbar
     * 右侧图标的点击
     */
    @OnClick(R.id.toolbar_righttv)
    void onToolbarRightClick() {
        if (courseName.getText().toString().trim().equals(kcmc)) {
            //名称没改变
            finish();
        } else {
            //发送请求  修改课程名称
            dao.modifyCourse(kcid, courseName.getText().toString().trim(), CodeConst.EXPAND_MODIFY_COURSE);
        }
    }
}
